package nextEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type UserOnlineEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	UserName string
	DeviceId string

	Params map[string]interface{} // 配置参数
	Extend map[string]interface{} // 扩展参数
}

type userOnlinePo struct {
	Id         int64                  `json:"id" db:"id"`                  // 对象id
	CreateTime int64                  `json:"createTime" db:"create_time"` // 创建时间
	UpdateTime int64                  `json:"updateTime" db:"update_time"` // 更新时间
	UserName   string                 `json:"username" db:"username"`      // 设备厂商名称
	DeviceId   string                 `json:"deviceId" db:"device_id"`     // 设备类型名
	Params     map[string]interface{} `json:"params" db:"params"`          // 配置参数
	Extend     map[string]interface{} `json:"extend" db:"extend"`          // 扩展参数
}

func (e *UserOnlineEntity) Type() string {
	return "UserOnlineEntity"
}

func (e *UserOnlineEntity) TableName() string {
	return "tb_user_online"
}

func (e *UserOnlineEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *UserOnlineEntity) Instance() *UserOnlineEntity {
	return &UserOnlineEntity{
		BaseEntity: new(commEntity.BaseEntity),
		Params:     make(map[string]interface{}),
		Extend:     make(map[string]interface{}),
	}
}

func (e *UserOnlineEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *UserOnlineEntity) NewPo() interface{} {
	return &userOnlinePo{
		Params: make(map[string]interface{}),
		Extend: make(map[string]interface{}),
	}
}

func (e *UserOnlineEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *UserOnlineEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.UserName)
	return p
}

func (e *UserOnlineEntity) BuildByJson(jsn *string) error {
	var po userOnlinePo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *UserOnlineEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *UserOnlineEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*userOnlinePo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Params = po.Params
	ptr.Extend = po.Extend
	ptr.UserName = po.UserName
	ptr.DeviceId = po.DeviceId
}

func (e *UserOnlineEntity) BuildToPo() interface{} {
	ptr := e
	po := userOnlinePo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Params = ptr.Params
	po.Extend = ptr.Extend
	po.UserName = ptr.UserName
	po.DeviceId = ptr.DeviceId

	return &po
}
